package io.renren.modules.gengyun.controller;


import io.renren.modules.gengyun.dao.PlanIndicatorDao;
import io.renren.modules.gengyun.entity.PlanIndicatorEntity;
import io.renren.modules.gengyun.service.PlanIndicatorService;
import io.renren.modules.gengyun.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("gengyun/PlanIndicator")
@CrossOrigin
public class PlanIndicatorController {

    @Autowired
    private PlanIndicatorService indicatorService;
    @PostMapping("/find")
    public Map<String,Object> find(Long id) throws IOException {
        System.out.println(id);
        PlanIndicatorEntity indicator=indicatorService.getIndicator(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(indicator,"succeed",
                "internal error",false);
        return resultMap;
    }


    @PostMapping("/add")
    public Map<String,Object> add(PlanIndicatorEntity indicator) throws IOException {
        System.out.println(indicator);
        int  flag =indicatorService.save(indicator);
        System.out.println(indicator.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(PlanIndicatorEntity indicator) throws IOException {
        System.out.println(indicator);
        int  flag =indicatorService.update(indicator);
        System.out.println(indicator.toString());
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/delete")
    public Map<String,Object> delete(Long id) throws IOException {
        int  flag =indicatorService.delete(id);
        Map<String,Object> resultMap = ResultUtils.getResponse(flag,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/all")
    public Map<String,Object>  all() throws IOException {
        List<Map<String,Object>> allList =  indicatorService.allList();
        Map<String,Object> resultMap = ResultUtils.getResponse(allList,"succeed",
                "internal error",false);
        return resultMap;
    }

    @PostMapping("/findByName")
    public Map<String,Object> find(String name) throws IOException {
        System.out.println(name);
        PlanIndicatorEntity indicator=indicatorService.getIndicatorByName(name);
        Map<String,Object> resultMap = ResultUtils.getResponse(indicator,"succeed",
                "internal error",false);
        return resultMap;
    }

}
